package org.dyndns.opendemogroup.optimizer.problems;

import java.util.Random;

/**
 * Implements the Rastrigin function, as described on page 3 of the assignment
 * #2 handout.
 */
public class Rastrigin extends EvolutionStrategyBase
{

	private final double a;

	public Rastrigin ( int memberSize, int a )
	{
		super ( memberSize );
		this.a = a;
	}

	/**
	 * @see EvolutionStrategyBase#computeFitness(Random)
	 */
	@Override
	public double computeFitness ( Random randomSource )
	{
		double fitness = partSize * a;
		for ( int i = 0; i < partSize; i++ )
		{
			final double xi = values[i];
			fitness += ( xi * xi - a * Math.cos ( 2 * Math.PI * xi ) );
		}
		return fitness;
	}

	/**
	 * @see EvolutionStrategyBase#determineParameterMaximums()
	 */
	@Override
	protected double[] determineParameterMaximums ( )
	{
		double[] result = new double[partSize];
		for ( int i = 0; i < partSize; i++ )
		{
			result[i] = 5.12;
		}
		return result;
	}

	/**
	 * @see EvolutionStrategyBase#determineParameterMinimums()
	 */
	@Override
	protected double[] determineParameterMinimums ( )
	{
		double[] result = new double[partSize];
		for ( int i = 0; i < partSize; i++ )
		{
			result[i] = -5.12;
		}
		return result;
	}

	/**
	 * @see EvolutionStrategyBase#determineSigmaMaximums()
	 */
	@Override
	protected double[] determineSigmaMaximums ( )
	{
		double[] result = new double[partSize];
		for ( int i = 0; i < partSize; i++ )
		{
			result[i] = 0.5;
		}
		return result;
	}

	/**
	 * @see EvolutionStrategyBase#determineSigmaMinimums()
	 */
	@Override
	protected double[] determineSigmaMinimums ( )
	{
		double[] result = new double[partSize];
		for ( int i = 0; i < partSize; i++ )
		{
			result[i] = -0.5;
		}
		return result;
	}

	/**
	 * @see EvolutionStrategyBase#isMaximizing()
	 */
	@Override
	public boolean isMaximizing ( )
	{
		return false;
	}

}
